"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : security-overflow
Case Name   : SQL 语句 ALTER SERVER 修改外部服务器的选项为一个超长值是否会造成缓冲区溢出
Create At   : 2023/06/05
@zou_jialiang0501162244
Description :
    1. 创建 postgres_fdw 扩展
    2. 创建外部服务器
    3. 修改外部服务器的 host 选项为超长值
Expect      :
    1. 成功
    2. 成功
    3. 成功
History     :
"""

import os
import unittest

from yat.test import Node
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class Overflow(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_node = Node('PrimaryDbUser')
        self.cons = Constant()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.fs_name = 'fs_memory_0006'

    def test_security(self):
        text1 = '-----step1:创建 postgres_fdw 扩展;expect:成功-----'
        self.log.info(text1)
        msg = self.pri_sh.execut_db_sql(
            'create extension if not exists postgres_fdw;')
        self.log.info(msg)
        self.assertIn(self.cons.create_extension_success, msg, f'执行失败:{text1}')

        text2 = '-----step2:创建外部服务器;expect:成功'
        self.log.info(text2)
        sql_cmd = f"drop server if exists {self.fs_name};" \
                  f"create server {self.fs_name} foreign data wrapper " \
                  f"postgres_fdw options (host '{self.pri_node.db_host}', " \
                  f"port '{self.pri_node.db_port}', " \
                  f"dbname '{self.pri_node.db_name}');"
        msg = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(msg)
        self.assertIn(self.cons.create_server_success, msg,
                      f'执行失败：{text2}')

        text3 = '-----step3:修改外部服务器的host选项为超长值;expect:成功-----'
        self.log.info(text3)
        host = 'a' * 16000
        msg = self.pri_sh.execut_db_sql(
            f"alter server {self.fs_name} options (set host '{host}');")
        self.log.info(msg)
        self.assertIn('ALTER SERVER', msg, f'执行失败:{text3}')

    def tearDown(self):
        text4 = '-----step4:清理数据;expect:成功-----'
        self.log.info(text4)
        sql_cmd = f"drop server if exists {self.fs_name};" \
                  f"drop extension if exists postgres_fdw cascade;"
        msg = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(msg)
        self.assertIn(self.cons.drop_server_success, msg, f"执行失败:{text4}")
        self.assertIn(self.cons.drop_extension_success, msg, f"执行失败:{text4}")
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
